package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.form.PageResponse;
import com.cskaoyan.shopping.service.IProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @ClassName: ProductListController
 * @Description: 显示所有商品信息
 * @Author: 瞳
 * @Date: 2022/6/17 20:20
 */

@RestController
public class ProductController {

    @Autowired
    IProductService productService;

    // http://124.71.209.81/shopping/goods?page=1&size=20&sort=1&priceGt=&priceLte=
    // sort=1,价格从低到高排序；sort=-1，价格从高到低排序；=，综合排序

    @GetMapping("/shopping/goods")
    public ResponseData getProductList(@RequestParam Map requestMap) {

        Integer page = Integer.parseInt((String) requestMap.get("page"));
        Integer size = Integer.parseInt((String) requestMap.get("size"));

        // sort可能为""
        String sort = (String) requestMap.get("sort");

        // 可能请求不携带cid
        String cidString = (String) requestMap.get("cid");
        Long cid;
        if (cidString == null) {
            cid = null;
        } else {
            cid = Long.parseLong(cidString);
        }

        // 转化可能导致失败
        Integer priceGt = null;
        Integer priceLte = null;
        try {
            // 可能最高价和最低价为""，若为""，则不予处理
            if (!"".equals(requestMap.get("priceGt"))) {
                String priceGtString = (String) requestMap.get("priceGt");
                if (priceGtString != null) {
                    priceGt = Integer.parseInt(priceGtString);
                }
            }

            if (!"".equals(requestMap.get("priceLte"))) {
                String priceLteString = (String) requestMap.get("priceLte");
                if (priceLteString != null) {
                    priceLte = Integer.parseInt(priceLteString);
                }
            }
        } catch (Exception e) {
            return new ResponseUtil().setErrorMsg("输入的价格区间有误！！");
        }

        AllProductRequest request = new AllProductRequest();
        request.setPage(page);
        request.setSize(size);
        request.setSort(sort);
        request.setCid(cid);
        request.setPriceGt(priceGt);
        request.setPriceLte(priceLte);

        AllProductResponse allProductResponse = productService.getAllProduct(request);

        PageResponse pageResponse = new PageResponse();
        pageResponse.setData(allProductResponse.getProductDtoList());
        pageResponse.setTotal(allProductResponse.getTotal());

        if (ShoppingRetCode.SUCCESS.getCode().equals(allProductResponse.getCode())) {
            return new ResponseUtil().setData(pageResponse);
        }
        return new ResponseUtil().setErrorMsg(allProductResponse.getMsg());
    }
}